home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MISC / SHELL.ARC / Shell / Other / !ShellDemo / c / Main < prev   
Encoding:
Text File  |  1994-11-04  |  6.3 KB  |  215 lines

  1. /*
  2. Example program which uses some of the the Shell_* functions.
  3. It displays a times-table in a window, along with various
  4. labels and an animated bar-graph.
  5. */
  6.  
  7.  
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <stdio.h>
  13. #include <math.h>
  14.  
  15.  
  16. #include "Desklib:Error.h"
  17. #include "Desklib:WimpSWIs.h"
  18.  
  19.  
  20. #include "Shell.RectSave.h"    /* For saving of data in rectangles                */
  21. #include "Shell.TextRect.h"    /* For displaying large amounts of text in a rect. in a window    */
  22. #include "Shell.Printf.h"    /* A substitute for printf.                    */
  23. #include "Shell.Array.h"    /* For displaying 2D arrays.                    */
  24. #include "Shell.FontLabel.h"    /* For displaying small 'labels' in an outline font.        */
  25. #include "Shell.Label.h"    /* For displaying small 'labels' in the system font.        */
  26. #include "Shell.BlockRct.h"    /* For displaying blocks which consist of small rectangles.    */
  27. #include "Shell.Extra.h"    /* For various low-level graphics utils.            */
  28. #include "Shell.BarGraph.h"    /* For a bar-graph display.                    */
  29. #include "Shell.PlainRect.h"    /* For saving part of a Shell window as a sprite        */
  30. #include "Shell.BarIcon.h"    /* For an icon on the iconbar, and a default menu.        */
  31. #include "Shell.SpriteRect.h"    /* For displaying a sprite in the window.            */
  32.  
  33.  
  34. #define PI 3.14159265
  35.  
  36.  
  37.  
  38.  
  39. static char    *TimesTableRedrawer( int x, int y, Shell_GeneralArrayInfo *info)
  40.     /* Returns a string which contains the contents of array location (x,y)    */
  41. {
  42. static    char s[64];
  43. sprintf( s, info->format, (x+1)*(y+1));
  44. return s;
  45. }
  46.  
  47.  
  48.  
  49.  
  50. static int    BlockRedrawer( int x, int y, int maxx, int maxy, void *data)
  51.     /* This helps plots a 2D gaussian by returning the colour of    */
  52.     /* the block at position (x,y).                    */
  53.  
  54. {
  55. double    dx = (double) x-maxx/2.0;
  56. double    dy = (double) y-maxy/2.0;
  57.  
  58. UNUSED( data);
  59.  
  60. return (int) (8 - 7.99 * exp( -0.5 * (dx*dx+dy*dy)/50 ));
  61. }
  62.  
  63.  
  64.  
  65.  
  66. #define Rnd() ( (double) rand() / ( RAND_MAX+1.0))
  67.     /* Returns random double y, 0 <= y <1            */
  68.  
  69. #define RndInt( x) ( (int) (Rnd() * ((double) x)) )
  70.     /* Returns random integer from { 0, 1, ... x-1 }.    */
  71.  
  72.  
  73. /* The size of the sprite    */
  74. #define SPRITE_X 512
  75. #define SPRITE_Y 512
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int    main()
  82. {
  83.  
  84. Shell_windblock    *wind;
  85. Shell_rectblock    *textrect, *barrect, *anglerect, *spriterect;
  86. int        i;
  87. clock_t        t = 0;
  88. int        bars[100];
  89.  
  90.  
  91. Shell_Init( /*taskname */ "Shell Demomstration", /*resources*/ "ShellDemo");
  92.     /* This calls Event_Initialise() etc.    */
  93.  
  94. Shell_AddBarIcon( "!shelldemo");
  95.  
  96. Shell_InitRectSave(            /* This registers a handler for clicks on any shell    */
  97.     "SaveAs",/*template name*/    /* window. If the click is Shift-Menu, and the pointer    */
  98.     3,    /* dragsprite    */    /* is over a savable rectangle, a save box is opened.    */
  99.     0,    /* ok        */    /* There is a template called 'SaveAs' in ShellEG's    */
  100.     1,    /* cancel    */    /* 'Templates' file.                    */
  101.     2    /* filename    */
  102.     );
  103.  
  104.  
  105. /* TextRectPrintf-ing to a NULL windblock opens a default window    */
  106. /* Shell_Printf is equivalent to Shell_TextRectPrintf( NULL, ...).        */
  107.  
  108. Shell_Printf( "This is the default text-output window.\n");
  109. Shell_Printf( "---------------------------------------\n\n");
  110. Shell_Printf( "Use Shift-Menu to save array/text rectangles...\n\n");
  111.  
  112.  
  113. wind        = Shell_OpenGFXWindow();
  114. textrect    = Shell_AddTextRect( wind, 30, -400, colour_BLACK, colour_YELLOW);
  115.  
  116. Shell_TextRectPrintf( textrect, "Some text\n");
  117. Shell_SetWindowTitle( wind->window, "Shell Demonstration");
  118.  
  119. Shell_FontLabel( wind, -100, 20, colour_BLACK, colour_GREY3, "Some anti-aliased text");
  120. Shell_FontLabel( wind, 800, Shell_TEXTYSIZE, colour_BLACK, colour_GREY1, "Times tables...");
  121.  
  122. Shell_AddGeneralArray(
  123.     wind,
  124.     800, 0,                /* Position of top-left of the array            */
  125.     12, 12,             /* x and y size of array.                */
  126.     9,                /* Width of array's columns (in chrs).            */
  127.     "%i",                /* This string is passed to the redrawer.        */
  128.     TimesTableRedrawer,        /* Fn which returns text for any element of the array.    */
  129.     NULL,                /* pointer that is passed to the redraw fn.        */
  130.     colour_BLACK, colour_ORANGE    /* fore/back colour of the array.            */
  131.     );
  132.  
  133. Shell_AddGeneralBlockRect( wind, 100, 100, 20, 20, 8, 8, BlockRedrawer, NULL);
  134. Shell_AddGeneralBlockRect( wind, 500, 100, 20, 20, 8, 8, BlockRedrawer, NULL);
  135.     /* Displays an exponential-decay square, using 'BlockRedrawer' which returns    */
  136.     /* the brightness of any block in the rectangle.                */
  137.  
  138.  
  139. barrect = Shell_AddBarGraph(
  140.     wind,
  141.     30, -200,             /* Position of bottom-left of bar graph            */
  142.     30,                 /* Number of bars in the graph.                */
  143.     16, 12,             /* Spacing and width of bars.                */
  144.     96,                 /* Maximum height of bars.                */
  145.     bars,                /* Pointer to an array of 30 integers for bar heights    */
  146.     colour_RED, colour_GREY2
  147.     );
  148.     /* 'bars' is an array of the heights of each bar. Later, this array will be changed    */
  149.     /* and the bar-graph rectangle redrawn to give animation.                */
  150.  
  151.  
  152.  
  153. anglerect = Shell_Label( wind, 30, -50, colour_BLACK, colour_GREY3, "Angle =    ");
  154. Shell_SetRectUpdateTime( anglerect, 0.5);
  155.  
  156.  
  157. /* Make a rect which can be saved as a sprite.    */
  158. Shell_MakeRectIcon(
  159.     Shell_AddPlainRect( wind, -150, -250, 550, 300),
  160.     colour_TRANSPARENT, colour_TRANSPARENT,
  161.     "r1" /* border */
  162.     );
  163.  
  164. Shell_Label( wind, -140, -300, colour_BLACK, colour_GREEN, "Try Shift-Menu inside the rectangle above here...");
  165.  
  166.  
  167.  
  168. /* Make a sprite in the window.    */
  169. spriterect = Shell_AddSpriteRect( wind, 800, 128, SPRITE_X, SPRITE_Y);
  170.  
  171.  
  172. /* The next bit animates a bar-graph...    */
  173. /* and plot circles into the sprite.    */
  174.  
  175. for (;;) for ( i=0; i<100; i++)    {
  176.  
  177.     int    a;
  178.     
  179.     double    angle = i/100.0 * 16 * PI;
  180.  
  181.     for ( a=0; a<30; a++)    {        /* Update the array of bar-heights    */
  182.         bars[a] = 48 + (int) ( 40 * ( sin( angle + a/2.0) * cos( angle/4 + a/3.0)));
  183.         }
  184.  
  185.     Shell_ForceRectUpdate( barrect);    /* Redraw the bargraph imediately without    */
  186.                         /* clearing the rectangle.            */
  187.  
  188.     Shell_ReLabelfSlow( anglerect, "Angle = %g", angle);
  189.         /* Only updates at speed set earlier.    */
  190.  
  191.     if ( clock()/CLOCKS_PER_SEC/10 != t/10)    {
  192.         t = clock()/CLOCKS_PER_SEC;
  193.         Shell_Printf( "\nTime since start is now %i seconds\n\n", t);
  194.         Shell_TextRectPrint( textrect, "Some more text...\n");
  195.         }
  196.  
  197.     /* Plot a random circle into the spriterect...    */
  198.         {
  199.         Shell_RedirectToSprite( spriterect);
  200.         Wimp_SetColour( RndInt( 16));
  201.         GFX_Move( RndInt( SPRITE_X), RndInt( SPRITE_Y));
  202.         GFX_Plot(
  203.             plot_CIRCLEFILL + ( RndInt(2) ? plot_DRAWRELINVERSE : plot_DRAWRELFORE),
  204.             RndInt( SPRITE_X/4), 0
  205.             );
  206.         Shell_UnRedirect();
  207.         Shell_ForceRectUpdate( spriterect);
  208.         }
  209.  
  210.     Shell_PollSlow();
  211.     }
  212.  
  213. return 0;
  214. }
  215.